package com.wushi.lenovo.asia5b.app_config;

import android.app.Activity;

import com.cloudinary.android.MediaManager;
import com.mvp.base.BaseApplication;
import com.mvp.common.GlobalValue;
import com.mvp.common.LocalManageUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.commonsdk.UMConfigure;
import com.wushi.lenovo.asia5b.BuildConfig;
import com.wushi.lenovo.asia5b.app_push.FireBasePushHelper;
import com.wushi.lenovo.asia5b.app_push.PushHelper;
import com.wushi.lenovo.asia5b.app_push.UMengPushHelper;

import java.util.Locale;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * ================================================
 * Created by ${chenyuexueer}
 * 时间： 2018/7/5.
 * 说明：MalayAsiaApplication
 * ================================================
 */
public class MalayAsiaApplication extends BaseApplication {
    private static final String BUGLYAPPID = "c3e4eaead9"; //腾讯的 bugly appid

    //各个平台的配置，建议放在全局Application或者程序入口
    {
//        PlatformConfig.setWeixin("wxeb9cbbdcf7846c37", "e44a92a991add73d13a60ca269e57a81");
        //没有用到友盟中的faceboo,所以注释掉
//        setFacebook("1475748862539872", "e5bcf3784d25df7340279fdd3a446cc2");
//        setInstagram("041e728afb3948a0b840491f1e3a99c2", "4376cc0779f94516b1f3150d504ee0af ");
    }


    @Override
    public void onCreate() {
        GlobalValue.isDebug = BuildConfig.DEBUG;
        GlobalValue.BASE_API_URL = BuildConfig.appType == 0 ? GlobalValue.BASE_API_URL_TEST : GlobalValue.BASE_API_URL_COM;
        GlobalValue.BASE_PAYMENT_THIRD_URL = BuildConfig.appType == 0 ? GlobalValue.BASE_PAYMENT_THIRD_URL_TEST : GlobalValue.BASE_PAYMENT_THIRD_URL_COM;
        GlobalValue.H5_Url = BuildConfig.appType == 0 ? GlobalValue.H5_Url_Test : GlobalValue.H5_Url_Com;
        super.onCreate();
        LocalManageUtil.init(this);
        boolean isFirstEnterApp = GlobalValue.isFirstLaunch();
        if (isFirstEnterApp) {//默认第一次安装使用英文状态  debug环境用中文,正式环境用英文
            LocalManageUtil.setAppLanguageSelect(!GlobalValue.isDebug);
        } else {//后续使用用户的设置
            LocalManageUtil.setAppLanguageSelect(LocalManageUtil.getAppLanguageSelect() == Locale.ENGLISH);
        }

        //初始化友盟
//        initUM();

        //设置线程的优先级,不与主线程抢资源
        new Thread() {
            @Override
            public void run() {
                //初始化云图片服务器
                MediaManager.init(sContext);
            }
        }.start();

        //初始化bugly
        initBugly();
        //初始化firebase推送
        FireBasePushHelper.initFireBase(this);


    }


    private void initBugly() {
        /**
         * 上下文
         * AppID
         * SDK调试模式开关：1、输出详细的Bugly SDK的Log
         *                                  2、每一条Crash都会被立即上报
         *                                  3、自定义日志将会在Logcat中输出。
         */
        CrashReport.initCrashReport(getApplicationContext(), BUGLYAPPID, true);
    }


    /**
     * 初始化友盟分享
     */
    public void initUM() {
        //开始注册友盟
//        UMShareAPI.get(this);
        /*
        // 参数一：当前上下文context；
        // 参数二：应用申请的Appkey（需替换）；
        // 参数三：渠道名称；
        // 参数四：设备类型，必须参数，传参数为UMConfigure.DEVICE_TYPE_PHONE则表示手机；传参数为UMConfigure.DEVICE_TYPE_BOX则表示盒子；默认为手机；
        // 参数五：Push推送业务的secret 填充Umeng Message Secret对应信息（需替换）
         */
        //线下/线上  0/1
        String umAppkey = BuildConfig.appType == 0 ? "5c0f2db6f1f5562e2e000086" : "59759de48f4a9d48be000f0c";
        //        String umAppkey = "59759de48f4a9d48be000f0c";
        String umMessageSecret = BuildConfig.appType == 0 ? "0f2e71089a9e009367e0b3d5f620a6ea" : "f4f16bfe11afc48815751d0bec3a036e";
        //        String umMessageSecret = "f4f16bfe11afc48815751d0bec3a036e";
        String qudao = BuildConfig.appType == 0 ? "fir" : "googleplay";
        UMConfigure.init(this, umAppkey, qudao, UMConfigure.DEVICE_TYPE_PHONE, umMessageSecret);
        UMConfigure.setLogEnabled(true);//控制SDK运行调试日志是否输出
    }

    public void initUMPush() {
        Observable.just(1)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        initUm2();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });

    }

    private void initUm2() {
        /*
        // 参数一：当前上下文context；
        // 参数二：应用申请的Appkey（需替换）；
        // 参数三：渠道名称；
        // 参数四：设备类型，必须参数，传参数为UMConfigure.DEVICE_TYPE_PHONE则表示手机；传参数为UMConfigure.DEVICE_TYPE_BOX则表示盒子；默认为手机；
        // 参数五：Push推送业务的secret 填充Umeng Message Secret对应信息（需替换）
         */
        //线下/线上  0/1
        String umAppkey = BuildConfig.appType == 0 ? "5c0f2db6f1f5562e2e000086" : "59759de48f4a9d48be000f0c";
        //        String umAppkey = "59759de48f4a9d48be000f0c";
        String umMessageSecret = BuildConfig.appType == 0 ? "0f2e71089a9e009367e0b3d5f620a6ea" : "f4f16bfe11afc48815751d0bec3a036e";
        //        String umMessageSecret = "f4f16bfe11afc48815751d0bec3a036e";
        String qudao = BuildConfig.appType == 0 ? "fir" : "googleplay";
        UMConfigure.init(this, umAppkey, qudao, UMConfigure.DEVICE_TYPE_PHONE, umMessageSecret);
        UMConfigure.setLogEnabled(true);//控制SDK运行调试日志是否输出
        UMengPushHelper.registUmengPush(MalayAsiaApplication.this);
    }

    public static Activity getEndActivity() {
        if (activityLinkedList.size() == 0) {
            return null;
        }
        return MalayAsiaApplication.activityLinkedList.get(MalayAsiaApplication.activityLinkedList.size() - 1);
    }

    @Override
    public String getRegistrationId() {
        return PushHelper.getRegistrationId();
    }
}
